<?php
/**
* エラーチェッククラス
**/
require_once( '../system/Class_M_DAO.php' );
class Class_M_MAP_GROUP_WEEK extends Class_M_DAO {
	//protected properties begin
	/**
	 *
	 * テーブル名
	 *
	 * @access    protected
	 * @var       string
	 *
	 */
	protected $_table  = "M_MAP_GROUP_WEEK";
	/**
	 *
	 * 主キー名
	 *
	 * @access    protected
	 * @var       string
	 *
	 */
	protected $_pkey   = "id";
	
	/**
	 *
	 * ソート順
	 *
	 * @access    protected
	 * @var       string
	 *
	 */
	 protected $_order = "map_no";
	 
	 protected $_cols  = array(
		"map_no"       => array(AOKI_VARTYPE_STR),
		"matrix_cd"    => array(AOKI_VARTYPE_STR)
	 );
	
	 protected $_checks  = array(
		"map_no"  => array( "EXIST","NUM" ),
	 );
	 
 	 public function get_matrix_chk_fields() {
		$sea_chk_fields  = array(
			"year4"           => array( "EXIST"),
			"week_cd"         => array( "EXIST")		
		);
	 return $sea_chk_fields;
	}
	 
	 public function search($where ,&$page = NULL ){
	 	//ここで変更がある場合、copy_to_week()ところも変更必要
		$fromtable = "  from M_MAP_GROUP a  inner join M_MAP_GROUP_WEEK b  on  a.map_no = b.map_no ";
		$sql       = " select a.map_no,a.sort,a.media_name,b.id,b.u_time,b.matrix_cd ".$fromtable ;
		$sqlCount  = " SELECT count(*) AS cnt ".$fromtable ;
		return  parent::search($where, $page, $sql, $sqlCount) ;
	}
	
	
	 public function searchDetails($where ,&$page = NULL ){
		$fromtable = " from M_MAP_GROUP_SHOP a  inner join M_MATRIX_TERM b  on  a.matrix_cd = b.matrix_cd  inner join M_MAP_GROUP c  on  a.map_no = c.map_no ";
		//$fromtable = $fromtable." inner join (select cast(shop_cd as char(5)) as shop_cd, shop_name,smx from M_SHOP union  all select mservice_cd as shop_cd, mservice_name as shop_name, '' as smx from M_MAP_SERVICE) d  on  a.shop_cd = d.shop_cd ";
		$fromtable = $fromtable." inner join (select cast(shop_cd as char(5)) as shop_cd, shop_name from M_SHOP union  all select mservice_cd as shop_cd, mservice_name as shop_name from M_MAP_SERVICE) d  on  a.shop_cd = d.shop_cd ";
		//$sql       = " select a.*,b.year4,b.week_cd, b.flier_date, c.sort,c.media_name,d.shop_cd,d.shop_name,d.smx ".$fromtable ;
		$sql       = " select a.*,b.year4,b.week_cd, b.flier_date, c.sort,c.media_cd,c.media_name,d.shop_cd,d.shop_name" .
				" ,(select  user_name FROM M_LOGIN where id  = a.u_user) as m_user_name ".$fromtable ;
		$sqlCount  = " SELECT count(*) AS cnt ".$fromtable ;
		
		$orderby   = " a.matrix_cd, a.map_no,a.seq ";
		
		return  parent::search($where, $page, $sql, $sqlCount,$orderby) ;
	}
	
	public function getShopCount($map_no, $matrix_cd) {
		$sql = "SELECT count(*)  as cnt  FROM M_MAP_GROUP_SHOP WHERE map_no = '{$map_no}' AND matrix_cd = '{$matrix_cd}' ";
		$this->_db->query( $sql );
		if( $this->_db->num_rows() ){
			$row  = $this->_db->fetch();
			return $row["cnt"];
			
		}else{
			return  null;
		}
	}
	
	public function getMaxWeek($map_no) {
		$sql = " SELECT max(a.matrix_cd) as matrix_cd from M_MAP_GROUP_WEEK a inner join M_MAP_GROUP b on a.map_no = b.map_no where a.map_no = '{$map_no}' ";
		$this->_db->query( $sql );
		if( $this->_db->num_rows() ){
			$row  = $this->_db->fetch();
			return $row["matrix_cd"];
			
		}else{
			return  null;
		}
	}
	
	public function  copy_shops($map_no, $from_week, $to_week) {
		$basefields = "map_no,shop_cd,group_no,mark,comm,congruent_map,congruent_ins,ins_load_ratio,memo,dummy,service";
		$intoFields = $basefields.",matrix_cd,u_time ,u_user,c_time,c_user";
		$fromFields = $basefields.",'{$to_week}',CURRENT_TIMESTAMP,'{$loginuser}' ".",CURRENT_TIMESTAMP,'{$loginuser}' ";
		
		$where = sprintf(" map_no = '%s' AND matrix_cd  = '%s'", $map_no, $from_week );
		$sql = "INSERT INTO M_MAP_GROUP_SHOP({$intoFields}) SELECT {$fromFields} from M_MAP_GROUP_SHOP where".$where;
		
		return $this->exec_sql($sql) ;
	}
	public function  copy_to_week( $where, $from_week, $to_week) {
		$loginuser = $_SESSION['ADMIN_LOGIN']['STAFF_SEQ'];
		$fromtable = "  from M_MAP_GROUP a  inner join M_MAP_GROUP_WEEK b  on  a.map_no = b.map_no ";
		$where = implode(' AND ', $where);
		$sqlfrom       = " select a.map_no ".$fromtable ." WHERE ".$where  ;
		
		$basefields = "map_no,shop_cd,group_no,mark,comm,congruent_map,congruent_ins,ins_load_ratio,memo,dummy,service,work_time";
		$intoFields = $basefields.",matrix_cd,u_time ,u_user,c_time,c_user";
		$fromFields = $basefields.",'{$to_week}' as matrix_cd,CURRENT_TIMESTAMP,'{$loginuser}' ".",CURRENT_TIMESTAMP,'{$loginuser}' ";
		
		$where =  " where a.matrix_cd ='{$from_week}' AND a.map_no in ({$sqlfrom})";
		
		$insertsql = "INSERT INTO  M_MAP_GROUP_SHOP({$intoFields}) SELECT {$fromFields} from M_MAP_GROUP_SHOP a ".$where;
		
		$deletesql = "DELETE FROM  M_MAP_GROUP_SHOP where matrix_cd ='{$to_week}' AND map_no in ({$sqlfrom}) ";
		
		
		if  ( $this->exec_sql($deletesql) ) {			
		}else {
			return false;
		}
		if  ( $this->exec_sql($insertsql) ) {
			return true;
		} else {
			return false;
		}

	}
	public function  del_map_no($map_no, $week) {
		$where = sprintf("where  map_no = '%s' AND matrix_cd  = '%s'", $map_no, $week );
		$sql =  " delete from M_MAP_GROUP_SHOP  ".$where;
		$this->begin_trans();
		if  ( $this->exec_sql($sql) ) {
			$sql =  " delete from M_MAP_GROUP_WEEK  ".$where;
			if  ( $this->exec_sql($sql) ) {
				$this->commit_trans();
				return true;
			} else {
				$this->rollback_trans();
				return false;
			}
		}else {
			$this->rollback_trans();
			return false;
		}
	}
	public function  del_sort($sortno, $week) {
		$where = sprintf("where  map_no in (select map_no  from M_MAP_GROUP where sort = '%s') AND matrix_cd  = '%s'", $sortno, $week );
		
		$sql =  " delete from M_MAP_GROUP_SHOP  ".$where;
		$this->begin_trans();
		if  ( $this->exec_sql($sql) ) {
			$sql =  " delete from M_MAP_GROUP_WEEK  ".$where;
			if  ( $this->exec_sql($sql) ) {
				$this->commit_trans();
				return true;
			} else {
				$this->rollback_trans();
				return false;
			}
		}else {
			$this->rollback_trans();
			return false;
		}
	}
	
	
	
}

?>